package com.collect.zambia.zhiScene.zhiCollection.zhiMember.zhiPerformance;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.zambia.R;
import com.collect.zambia.zhiAdater.zhiLevelDetail.ZHILevelDetailAdjuster;
import com.collect.zambia.zhiBase.BaseActivity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRGroupUserEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRPerformanceDayDetailEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRPerformanceListEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISPerfermanceEntity;
import com.collect.zambia.zhiGlobal.zhiAppSettings.ZHIProjectConstants;
import com.collect.zambia.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.zambia.zhiHelper.ZHIMainProcessHelper;
import com.collect.zambia.zhiScene.zhiCollection.zhiMember.zhiPerformance.zhiSort.ZHIMemberPerformanceCompareByDateASC;
import com.collect.zambia.zhiScene.zhiCollection.zhiMember.zhiPerformance.zhiSort.ZHIMemberPerformanceCompareByDateDESC;
import com.collect.zambia.zhiScene.zhiWidget.zhiFilterPerformance.ZHIFilterPerformanceClickListener;
import com.collect.zambia.zhiScene.zhiWidget.zhiFilterPerformance.ZHIFilterPerformanceMember;
import com.collect.zambia.zhiUtils.StringUtil;
import com.collect.zambia.zhiUtils.ZHICalendarUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_PERFORMANCE_MEMBER_OLD)
public class ZHIPerformanceMemberActivity extends BaseActivity<ZHIPerformanceMemberPresenter> implements ZHIPerformanceMemberContract.View {

    @BindView(R.id.drawer_layout)
    DrawerLayout drawer_layout;

    @BindView(R.id.drawer_filter_performance)
    ZHIFilterPerformanceMember drawer_filter_performance;

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.tv_cumulative_bonus)
    TextView tv_cumulative_bonus;
    @BindView(R.id.tv_cumulative_bonus_date)
    TextView tv_cumulative_bonus_date;

    @BindView(R.id.tv_level_b1_amount)
    TextView tv_level_b1_amount;
    @BindView(R.id.tv_level_b2_amount)
    TextView tv_level_b2_amount;
    @BindView(R.id.tv_level_b3_amount)
    TextView tv_level_b3_amount;

    @BindView(R.id.rl_still_need_tip)
    RelativeLayout rl_still_need_tip;

    @BindView(R.id.rl_b1_collect_oder_num)
    RelativeLayout rl_b1_collect_oder_num;
    @BindView(R.id.tv_b1_collect_oder_num)
    TextView tv_b1_collect_oder_num;

    @BindView(R.id.rl_b2_collect_oder_num)
    RelativeLayout rl_b2_collect_oder_num;
    @BindView(R.id.tv_b2_collect_oder_num)
    TextView tv_b2_collect_oder_num;

    @BindView(R.id.rl_b3_collect_oder_num)
    RelativeLayout rl_b3_collect_oder_num;
    @BindView(R.id.tv_b3_collect_amount)
    TextView tv_b3_collect_amount;

    @BindView(R.id.tv_level)
    TextView tv_level;
    @BindView(R.id.tv_level_date)
    TextView tv_level_date;
    @BindView(R.id.tv_collect_num_des)
    TextView tv_collect_num_des;
    @BindView(R.id.tv_collect_num)
    TextView tv_collect_num;
    @BindView(R.id.tv_collect_rate)
    TextView tv_collect_rate;
    @BindView(R.id.tv_collect_bonus)
    TextView tv_collect_bonus;

    @BindView(R.id.tv_detail_case_num)
    TextView tv_detail_case_num;
    @BindView(R.id.tv_detail_collected_num)
    TextView tv_detail_collected_num;

    @BindView(R.id.rc_detail_list)
    RecyclerView rc_detail_list;

    @BindView(R.id.iv_note)
    ImageView iv_note;
    @BindView(R.id.iv_filter)
    ImageView iv_filter;

    @BindView(R.id.tv_group_members)
    TextView tv_group_members;
    @BindView(R.id.tv_monthy_ranking)
    TextView tv_monthy_ranking;
    @BindView(R.id.tv_daily_ranking)
    TextView tv_daily_ranking;

    @BindView(R.id.rl_part_repay_num_bg)
    RelativeLayout rl_part_repay_num_bg;
    @BindView(R.id.tv_part_repay_num)
    TextView tv_part_repay_num;
    @BindView(R.id.iv_part_repay)
    ImageView iv_part_repay;
    @BindView(R.id.tv_part_repay_num_des)
    TextView tv_part_repay_num_des;

    @BindView(R.id.iv_sort)
    ImageView iv_sort;

    ZHILevelDetailAdjuster mZHILevelDetailAdjuster = new ZHILevelDetailAdjuster();
    private ArrayList<ZHIRPerformanceDayDetailEntity> mMonthDetailDataArrayList;

    private boolean mHadFirstLoad;

    /**
     * 是否为降序
     */
    private boolean mIsArraySortDesNow = true;

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_perfermance_member;
    }

    @Override
    protected void initView() {
        if (!mHadFirstLoad) {
            mHadFirstLoad = true;
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            iv_note.setOnClickListener(v -> finish());

            iv_filter.setOnClickListener(v -> {
                onShowFilterPerformanceMember();
            });

            drawer_filter_performance.setZHIPerformanceFilterClickListener(new ZHIFilterPerformanceClickListener() {
                @Override
                public void onConfirmClick() {
                    if (drawer_layout.isDrawerOpen(drawer_filter_performance)) {
                        drawer_layout.closeDrawer(drawer_filter_performance);
                    }

                    refreshData();
                }

                @Override
                public void onCloseClick() {
                    if (drawer_layout.isDrawerOpen(drawer_filter_performance)) {
                        drawer_layout.closeDrawer(drawer_filter_performance);
                    }
                }
            });

            iv_sort.setOnClickListener(v -> {
                Log.d(TAG, "initView: " + mMonthDetailDataArrayList.size());
                if (mMonthDetailDataArrayList != null) {
                    if (mIsArraySortDesNow) {
                        Collections.sort(mMonthDetailDataArrayList, new ZHIMemberPerformanceCompareByDateDESC());
                        mZHILevelDetailAdjuster.notifyDataSetChanged();
                        mIsArraySortDesNow = false;
                    } else {
                        Collections.sort(mMonthDetailDataArrayList, new ZHIMemberPerformanceCompareByDateASC());
                        mZHILevelDetailAdjuster.notifyDataSetChanged();
                        mIsArraySortDesNow = true;
                    }
                }
            });

            rl_part_repay_num_bg.setOnClickListener(v -> {
                onReloadDailyListForPartRepayFlag();
            });

            tv_part_repay_num.setOnClickListener(v -> {
                onReloadDailyListForPartRepayFlag();
            });

            iv_part_repay.setOnClickListener(v -> {
                onReloadDailyListForPartRepayFlag();
            });

            tv_part_repay_num_des.setOnClickListener(v -> {
                onReloadDailyListForPartRepayFlag();
            });


            srl_swipe_layout.setOnRefreshListener(() -> {
                try {
                    long gtime = System.currentTimeMillis();

                    if (gtime - ZHIMainProcessHelper.mLastRequestBunsApiTime >= 10000L) {
                        initData(false);
                    } else {
                        Log.d(TAG, "setOnRefreshListener: in 5s");
                        srl_swipe_layout.setRefreshing(false);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            rc_detail_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
            rc_detail_list.setAdapter(mZHILevelDetailAdjuster);

            mZHILevelDetailAdjuster.setOnZHILevelDetailAdjusterItemClickListener(perfermanceEntity -> {

            });

            initData(true);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onGetLatestPerfermanceListV3(ZHIRPerformanceListEntity zhirPerformanceListEntity, ZHISPerfermanceEntity zhisPerfermanceEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            ///////////////////////
            tv_cumulative_bonus.setText(zhirPerformanceListEntity.getMonthData().getCurrentMonthBonusFormatWithoutFinceUnit());
            tv_cumulative_bonus_date.setText(zhirPerformanceListEntity.getMonthData().getMonth());

            tv_level_b1_amount.setText(zhirPerformanceListEntity.getMonthData().getB1().getBonusFormat());
            tv_level_b2_amount.setText(zhirPerformanceListEntity.getMonthData().getB2().getBonusFormat());
            tv_level_b3_amount.setText(zhirPerformanceListEntity.getMonthData().getB3().getBonusFormat());

            ///////////////////////
            tv_group_members.setText(zhirPerformanceListEntity.getRankData().getGroupPeopleNum());
            tv_monthy_ranking.setText(zhirPerformanceListEntity.getRankData().getGroupMonthRank());
            tv_daily_ranking.setText(zhirPerformanceListEntity.getRankData().getGroupTodayRank());

            ///////////////////////
            if (zhirPerformanceListEntity.getTodayData().getDataList().size() > 0) {
                ZHIRPerformanceDayDetailEntity zhirPerformanceDayDetailEntity = zhirPerformanceListEntity.getTodayData().getDataList().get(0);

                //暂时隐藏-start
                if (!zhirPerformanceListEntity.getMonthData().getB1().isCompleteFlag()) {
                    if (zhirPerformanceDayDetailEntity.isLevelB1()) {
                        rl_still_need_tip.setVisibility(View.VISIBLE);
                        rl_b1_collect_oder_num.setVisibility(View.VISIBLE);

                        int b1remaindNum = zhirPerformanceListEntity.getMonthData().getB1().getRemainNumInt();
                        int todayb1num = zhirPerformanceDayDetailEntity.getRepayNumInt();
                        int b1remindNumEdge = b1remaindNum - todayb1num;
                        tv_b1_collect_oder_num.setText(b1remindNumEdge + "");
//                        tv_b1_collect_oder_num.setText(zhirPerformanceListEntity.getMonthData().getB1().getRemainNum());

                        rl_b2_collect_oder_num.setVisibility(View.GONE);
                        rl_b3_collect_oder_num.setVisibility(View.GONE);
                    }
                }

                if (!zhirPerformanceListEntity.getMonthData().getB2().isCompleteFlag()) {
                    if (zhirPerformanceDayDetailEntity.isLevelB2()) {
                        rl_still_need_tip.setVisibility(View.VISIBLE);
                        rl_b2_collect_oder_num.setVisibility(View.VISIBLE);

                        int b2remaindNum = zhirPerformanceListEntity.getMonthData().getB2().getRemainNumInt();
                        int todayb2num = zhirPerformanceDayDetailEntity.getRepayNumInt();
                        int b2remindNumEdge = b2remaindNum - todayb2num;
                        tv_b2_collect_oder_num.setText(b2remindNumEdge + "");
//                        tv_b2_collect_oder_num.setText(zhirPerformanceListEntity.getMonthData().getB2().getRemainNum());

                        rl_b1_collect_oder_num.setVisibility(View.GONE);
                        rl_b3_collect_oder_num.setVisibility(View.GONE);
                    }
                }

                if (!zhirPerformanceListEntity.getMonthData().getB3().isCompleteFlag()) {
                    if (zhirPerformanceDayDetailEntity.isLevelB3()) {
                        rl_still_need_tip.setVisibility(View.VISIBLE);
                        rl_b3_collect_oder_num.setVisibility(View.VISIBLE);
                        tv_b3_collect_amount.setText(zhirPerformanceListEntity.getMonthData().getB3().getRemainAmountFormat());

                        BigDecimal b3remaindNum = zhirPerformanceListEntity.getMonthData().getB3().getRemainAmountBigDecimal();
                        BigDecimal todayb3num = zhirPerformanceDayDetailEntity.getRepayAmountBigDecimal();
                        BigDecimal b3remindNumEdge = b3remaindNum.subtract(todayb3num);
                        tv_b3_collect_amount.setText(StringUtil.numberFormatter00(b3remindNumEdge.stripTrailingZeros().toPlainString()) + ZHIProjectConstants.RS_FLAG);
//                        tv_b3_collect_amount.setText(zhirPerformanceListEntity.getMonthData().getB3().getRemainNum());

                        rl_b1_collect_oder_num.setVisibility(View.GONE);
                        rl_b2_collect_oder_num.setVisibility(View.GONE);
                    }
                }
                //暂时隐藏-end
            } else {
                rl_still_need_tip.setVisibility(View.GONE);
            }

            ///////////////////////
            tv_level_date.setText(zhirPerformanceListEntity.getTodayData().getToday());

            if (zhirPerformanceListEntity.getTodayData().getDataList().size() > 0) {
                ZHIRPerformanceDayDetailEntity zhirPerformanceDayDetailEntity = zhirPerformanceListEntity.getTodayData().getDataList().get(0);
                tv_collect_bonus.setText(zhirPerformanceDayDetailEntity.getBonus());
                tv_level.setText(zhirPerformanceDayDetailEntity.getLeven());

                boolean isB2Level = (zhirPerformanceDayDetailEntity.getLeven().equalsIgnoreCase("B2") || zhirPerformanceDayDetailEntity.getLeven().equalsIgnoreCase("M2"));
                rl_part_repay_num_bg.setVisibility(isB2Level ? View.VISIBLE : View.INVISIBLE);
                tv_part_repay_num.setText(zhirPerformanceListEntity.getPartRepayNum());

                if (zhirPerformanceDayDetailEntity.isLevelB3()) {
                    tv_collect_num_des.setText(getString(R.string.collect_amount));
                    tv_collect_num.setText(zhirPerformanceDayDetailEntity.getRepayAmountFormat());
                    tv_collect_rate.setText(zhirPerformanceDayDetailEntity.getRepayAmountRate());
                } else {
                    tv_collect_num_des.setText(getString(R.string.collect_num));
                    tv_collect_num.setText(zhirPerformanceDayDetailEntity.getRepayNum());
                    tv_collect_rate.setText(zhirPerformanceDayDetailEntity.getRepayNumRate());
                }
            }

            ///////////////////////
            if (zhirPerformanceListEntity.getTodayData().getDataList().size() > 0) {
                ZHIRPerformanceDayDetailEntity zhirPerformanceDayDetailEntity = zhirPerformanceListEntity.getTodayData().getDataList().get(0);
                if (zhirPerformanceDayDetailEntity.isLevelB3()) {
                    tv_detail_case_num.setText(getString(R.string.case_amount));
                    tv_detail_collected_num.setText(getString(R.string.collected_amount));
                } else {
                    tv_detail_case_num.setText(getString(R.string.case_num));
                    tv_detail_collected_num.setText(getString(R.string.collected_num));
                }
            }
            mMonthDetailDataArrayList = zhirPerformanceListEntity.getMonthDetailData();
            Collections.sort(mMonthDetailDataArrayList, new ZHIMemberPerformanceCompareByDateASC());
            mZHILevelDetailAdjuster.setNewData(mMonthDetailDataArrayList);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetGroupUsers(ArrayList<ZHIRGroupUserEntity> zhirGroupUserEntityArrayList) {
        srl_swipe_layout.setRefreshing(false);

    }

    public void initData(boolean showDialog) {

        ZHIMainProcessHelper.mLastRequestBunsApiTime = System.currentTimeMillis();

        ZHISPerfermanceEntity zhisPerfermanceEntity = getCaseSearchParams();
        if (zhisPerfermanceEntity == null) {
            srl_swipe_layout.setRefreshing(false);
            return;
        }
        assert mPresenter != null;
        mPresenter.getLatestPerfermanceListV3(getActivity(), zhisPerfermanceEntity, showDialog);
    }

    public void refreshData() {
        initData(true);
    }

    private ZHISPerfermanceEntity getCaseSearchParams() {
        return onGetPerfermanceFilterMemberParams();
    }

    public void onShowFilterPerformanceMember() {
        if (drawer_filter_performance != null) {
            if (drawer_layout.isDrawerOpen(drawer_filter_performance)) {
                drawer_layout.closeDrawer(drawer_filter_performance);
            } else {
                drawer_layout.openDrawer(drawer_filter_performance);
            }
        }
    }

    public void onReloadDailyListForPartRepayFlag() {

    }

    public ZHISPerfermanceEntity onGetPerfermanceFilterMemberParams() {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
        zhisPerfermanceEntity.setLeven(drawer_filter_performance.getLevel());
        zhisPerfermanceEntity.setMonth(drawer_filter_performance.getMonth());
        zhisPerfermanceEntity.setStartDate(ZHICalendarUtil.getCurMonthFirstDayFormatString());
        zhisPerfermanceEntity.setEndDate(ZHICalendarUtil.getDateStringAfterDate(ZHICalendarUtil.getCurMonthCurDayFormatString(), 1));
        zhisPerfermanceEntity.setSysLoginName(drawer_filter_performance.getMember());
        return zhisPerfermanceEntity;
    }

    public ZHISPerfermanceEntity onGetPerfermanceFilterAmountParams() {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
//        zhisPerfermanceEntity.setLeven(drawer_filter_performance.getLevel());
//        zhisPerfermanceEntity.setEndDate(drawer_filter_performance.getDueEndDate());
//        zhisPerfermanceEntity.setStartDate(drawer_filter_performance.getDudStartDate());
//        zhisPerfermanceEntity.setLeaderUserId(drawer_filter_performance.getMember());
        return zhisPerfermanceEntity;
    }

    public ZHISPerfermanceEntity onGetPerfermanceFilterOrderParams() {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
//        zhisPerfermanceEntity.setLeven(drawer_filter_performance.getLevel());
//        zhisPerfermanceEntity.setEndDate(drawer_filter_performance.getDueEndDate());
//        zhisPerfermanceEntity.setStartDate(drawer_filter_performance.getDudStartDate());
//        zhisPerfermanceEntity.setLeaderUserId(drawer_filter_performance.getMember());
        return zhisPerfermanceEntity;
    }

}
